package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    //需要远程调用对象
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private CategoryFeignClient categoryFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private RedissonClient redissonClient;

    @Override
    public Map<String, Object> getItem(Long albumId) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();
        //
//        RBloomFilter<Object> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
//        //判断布隆过滤器是否存在
//        if(!bloomFilter.contains(albumId)){
//            log.error("专辑id不存在{}", albumId);
//            return map;
//        }
        //多线程执行使用异步编排
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //远程调用
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult, "获取专辑信息失败");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo, "获取专辑信息失败");
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, threadPoolExecutor).exceptionally(throwable -> {
            log.error("查询结果集失败", throwable);
            return null;
        });
        CompletableFuture<Integer> categoryCompletableFuture = albumInfoCompletableFuture.thenApplyAsync(albumInfo -> {
            Result<BaseCategoryView> categoryView = categoryFeignClient.getCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(categoryView, "查询分类数据失败");
            BaseCategoryView baseCategoryView = categoryView.getData();
            Assert.notNull(baseCategoryView, "查询分类数据失败");
            map.put("baseCategoryView", baseCategoryView);
            return 1;
        }, threadPoolExecutor).exceptionally(throwable -> {
            log.error("查询分类数据失败", throwable);
            return 0;
        });
        CompletableFuture<Integer> userCompletableFuture = albumInfoCompletableFuture.thenApplyAsync((albumInfo) -> {
            Result<UserInfoVo> userInfo = userInfoFeignClient.getUserInfo(albumInfo.getUserId());
            Assert.notNull(userInfo, "查询用户信息失败");
            UserInfoVo userInfoVo = userInfo.getData();
            Assert.notNull(userInfoVo, "查询用户信息失败");
            map.put("announcer", userInfoVo);
            return 1;
        }, threadPoolExecutor).exceptionally(throwable -> {
            log.error("查询用户信息失败", throwable);
            return 0;
        });
        CompletableFuture<Integer> statVoCompletableFuture = albumInfoCompletableFuture.thenApplyAsync((albumInfo) -> {
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatVo(albumInfo.getId());
            Assert.notNull(albumStatVoResult, "查询专辑统计数据失败");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "查询专辑统计数据失败");
            map.put("albumStatVo", albumStatVo);
            return 1;
        }, threadPoolExecutor).exceptionally(throwable -> {
            log.error("查询专辑统计数据失败", throwable);
            return 0;
        });

        //判断是否都查到
        try {
            if (null == albumInfoCompletableFuture.get()
                    || 0 == categoryCompletableFuture.get()
                    || 0 == userCompletableFuture.get()
                    || 0 == statVoCompletableFuture.get()) {
                return null;
            }
        } catch (Exception e) {
            log.error("查询结果集失败", e.getMessage(), e);
        }
        //  多任务组合;
        CompletableFuture.allOf(
                albumInfoCompletableFuture,
                categoryCompletableFuture,
                userCompletableFuture,
                statVoCompletableFuture).join();
        //  返回数据
        return map;
    }
}
